const { shopData, times } = require('./gameData')
const EventServices = require('../util/EventServices');
const { msg } = require('../game/alert');
cc.Class({
    extends: cc.Component,

    properties: {
       shopList:cc.Node,//挂载对象
       tmpPrefab:cc.Prefab,
       tmpTime:0,

    },
    // LIFE-CYCLE CALLBACKS:
    update (dt) {
        this.tmpTime += dt;
        if(this.tmpTime >= 2){
            this.updateShop()

        }
        
    },
    onLoad() {
        //每天初始化广告
        let date = new Date();
        //如果不一样，就初始化一下
        let month = date.getMonth() + 1; //当前的月份
        let day = date.getDate(); //当前的天数
        if(day != times.lastTime.day) {
            //说明日期变了
            times.lastTime.month = month;
            times.lastTime.day = day;
            times.count = 4;
        }else if(month != times.lastTime.month) {
            times.lastTime.month = month;
            times.lastTime.day = day;
            times.count = 4;
        }
        msg("今日广告剩余" + times.count)
        EventServices.subscribe('shopUpdate',(shop, i) => {
            console.log('收到消息');
            shopData.shopList[shop].foodList[i].sales ++;
            shopData.shopList[shop].foodList[i].stock --;
        });

    },
    onEnable () {
        this.loadShop();
        cc.log('店铺列表');
    },
    loadShop(){
            let data = shopData.shopList;
            this.shopList.children.forEach(val=>{
                val.destroy();
            })
            for (var i = 0; i <data.length; i++) {
                if(data[i].status == 1 || data[i].status == 2){
                    let item  = cc.instantiate(this.tmpPrefab);
                    this.shopList.addChild(item);
                    item.getChildByName("shopName").getComponent(cc.Label).string = `${data[i].shopName}`
                    item.getChildByName("workers").getComponent(cc.Label).string = `员工：${data[i].workers}`
                    item.getChildByName("income").getComponent(cc.Label).string = `收入：${data[i].income}`
                    item.getChildByName("status").getComponent(cc.Label).string = `状态：${data[i].status == 1? "营业中":"待装修"}`
                    item.getChildByName("money").getComponent(cc.Label).string = `账户：${data[i].money}`
                    item.getChildByName("customer").getComponent(cc.Label).string = `顾客：${data[i].customer}`
                    item.id = `${data[i].id}`
                }
            }
    },
    updateShop(){
        let data = shopData.shopList;
        let index = 0;
        data.forEach((val)=>{
           if(val.status == 1 || val.status == 2){
                let tmp =  this.shopList.children[index];
                tmp.getChildByName("customer").getComponent(cc.Label).string = `顾客：${val.customer}`;
                tmp.getChildByName("workers").getComponent(cc.Label).string = `员工：${val.workers}`
                tmp.getChildByName("income").getComponent(cc.Label).string = `收入：${getOmitNum(val.income,2)}`
                tmp.getChildByName("money").getComponent(cc.Label).string = `账户：${getOmitNum(val.money, 2)}`
                index++;
           }
        })
    },
    addShop(){
        
    }
    // update (dt) {},
});
